Tag Archives: homeassistant

Home Assistant Speech and More

I made my own Mqtt to speech thingy in the past.
Sending a text to a mqtt topic would be picked up by my domoticz raspberry and using a bash script the topic payload was converted to speech and being played on a connected speaker.

This speaker migrated to my Home Assistant NUC.
So i changed the speech engine.

Beside this migration, i’ve started using the HA voice assistant capabilities.
This was a major impact/project in 2023.

I’m not going to talk about configuring this .. There are many good YT tutorials and forum topics about this.

https://www.home-assistant.io/voice_control/thirteen-usd-voice-remote/

This is the device: a ESP32 pico, Microphone, leds and Speaker are being used for this sound assistant.
(It uses ESPHOME)

Back to the speaker being hooked-up to my HA NUC.

Install the addon PicoTTS (speech synthesis)

configuration.yaml

# Text to speech
tts:
  - platform: picotts
# My part
input_text:
  mqttspeech:
    name: mqttspeech
    initial: ""
    

Then install notified addon

Add a text field to your dashboard …

HLK-LD2410B with a Wemos Mini D1 (v4) connected to Home Assistant using ESPHome

UPDATED: 20240419

LD2410 is a high-sensitivity 24GHz human presence status sensing module developed by Hi-link. Its working principle is to use FMCW frequency-modulated continuous waves to detect human targets in the set space. Combined with radar signal processing and precise human body sensing algorithms, it realizes high-sensitivity human presence status sensing, and can identify human bodies in motion and stationary states. And auxiliary information such as the distance of the target can be calculated.

This product is mainly used in indoor scenes to sense whether there is a moving or micro-moving human body in the area, and output the detection results in real time. The farthest sensing distance can reach 5 meters, and the distance resolution is 0.75m. Provides a visual configuration tool, which can easily configure the sensing distance range, sensing sensitivity in different intervals and unmanned delay time, etc., to adapt to different specific application needs.

Support GPIO and UART output, plug and play, and can be flexibly applied to different smart scenarios and terminal products.

There are 3 versions: Without Bluetooth, with Bluetooth (B version) and a C version which uses the standard pin distance.
The other ones are a pain in the *ss to solder!

Got a Bluetooth version? See end of post!

When searching for examples, I noticed that many had issues getting this working.
Let me be clear, it wasn’t working for me the first time.
Things i’ve learned:

  • Flash the first initial ESPHome using a USB cable, after that you can connect the module and flash OTA
  • Do not use the standard Uart RX/TX, it didn’t work for me. And messes-up the logging over USB (See baudrate: 0 to turn this off)
  • When connecting D7/D8 and this signal gets pulled down, the wemos won’t boot. (Running wifi connections gets interrupted)
    This is also a sign that RX/TX is switched around!
  • Measure and make sure you have a good, stable 5V power to your LD2410

Here is a post about the RCWL-0516, a similar project, but this one can’t measure distances and person detection won’t work when a person is not moving.

https://www.henriaanstoot.nl/2022/11/14/radar-module-rcwl-0516-with-mqtt/

Steps to take:

  • Flash ESPHome over USB
  • Detect and add using Home Assistant
  • Edit and upload code (below)
  • Connect the sensor

Wire it up like this:

20240419 image corrected

ESPHome YAML:

Parts i’ve changed:
board: Changed from esp-1?? to d1_mini
logger: baud_rate: 0
tx_pin and rx_pin

esphome:
  name: ld2410-1
  friendly_name: ld2410-1

esp8266:
  board: d1_mini

# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ld2410-1 Fallback Hotspot"
    password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

captive_portal:
    
ld2410:
  id: ld2410_radar
  
uart:
  tx_pin: GPIO15
  rx_pin: GPIO13
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

number:
  - platform: ld2410
    timeout:
      name: Radar Timeout
    max_move_distance_gate:
      name: Radar Max Move Distance
    max_still_distance_gate:
      name: Radar Max Still Distance
    g0:
      move_threshold:
        name: g0 move threshold
      still_threshold:
        name: g0 still threshold
    g1:
      move_threshold:
        name: g1 move threshold
      still_threshold:
        name: g1 still threshold
    g2:
      move_threshold:
        name: g2 move threshold
      still_threshold:
        name: g2 still threshold
    g3:
      move_threshold:
        name: g3 move threshold
      still_threshold:
        name: g3 still threshold
    g4:
      move_threshold:
        name: g4 move threshold
      still_threshold:
        name: g4 still threshold
    g5:
      move_threshold:
        name: g5 move threshold
      still_threshold:
        name: g5 still threshold
    g6:
      move_threshold:
        name: g6 move threshold
      still_threshold:
        name: g6 still threshold
    g7:
      move_threshold:
        name: g7 move threshold
      still_threshold:
        name: g7 still threshold
    g8:
      move_threshold:
        name: g8 move threshold
      still_threshold:
        name: g8 still threshold

binary_sensor:
  - platform: ld2410
    has_target:
      name: Radar Target
      id: radar_has_target
    has_moving_target:
      name: Radar Moving Target
    has_still_target:
      name: Radar Still Target
button:
  - platform: ld2410
    factory_reset:
      name: "factory reset"
    restart:
      name: "restart"
    query_params:
      name: query params

sensor:
  - platform: ld2410
    moving_distance:
      name: Radar Moving Distance
      id: moving_distance
    still_distance:
      name: Radar Still Distance
      id: still_distance
    moving_energy:
      name: Radar Move Energy
    still_energy:
      name: Radar Still Energy
    detection_distance:
      name: Radar Detection Distance
      id: radar_detection_distance
    g0:
      move_energy:
        name: g0 move energy
      still_energy:
        name: g0 still energy
    g1:
      move_energy:
        name: g1 move energy
      still_energy:
        name: g1 still energy
    g2:
      move_energy:
        name: g2 move energy
      still_energy:
        name: g2 still energy
    g3:
      move_energy:
        name: g3 move energy
      still_energy:
        name: g3 still energy
    g4:
      move_energy:
        name: g4 move energy
      still_energy:
        name: g4 still energy
    g5:
      move_energy:
        name: g5 move energy
      still_energy:
        name: g5 still energy
    g6:
      move_energy:
        name: g6 move energy
      still_energy:
        name: g6 still energy
    g7:
      move_energy:
        name: g7 move energy
      still_energy:
        name: g7 still energy
    g8:
      move_energy:
        name: g8 move energy
      still_energy:
        name: g8 still energy

Bluetooth:

I’ve connected this HLK-DL2410B to Home Assistant before using Bluetooth. But I wanted to get them connected using Wifi.

You can install an App on your phone to connect to the sensor when powered on.
This way you can test the device, but also upgrade the firmware and make adjustments!

Just enable engineering mode and click more.

Testing another baud rate and upgrading the firmware:

Home Assistant ESPHome with toggle for interval

Posted because I could not find a good example on the interwebs.

Below creates a virtual HA button which toggles a blinking led.
(button and variables are called eprint for another function, change to something meaningful. )

Home Assistant virtual mqtt switch (configuration.yml)

See switch part

mqtt:
 light:    
  - name: "KitchenOutside"
    payload_on: "99"
    payload_off: "0"
    unique_id: "KitchenOutside"
    brightness_scale: "99"
    brightness_state_topic: ha433/kitchenoutside/brightcontrol
    brightness_command_topic: ha433/kitchenoutside/brightcontrol
    state_topic: ha433/kitchenoutside/brightcontrol
    command_topic: ha433/kitchenoutside/control
    optimistic: false
    on_command_type: brightness
#-----8<-------------snip ########### EXAMPLES light and sensor
 sensor:
  - name: "LivingTemperature"
    state_topic: "home/8461/payload"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.svalue1 }}"
  - name: "LivingHumidity"
    state_topic: "home/8461/payload"
    unit_of_measurement: "%"
    value_template: "{{ value_json.svalue2 }}"
#--------8<--- snip ################################ ONLY PART BELOW NEEDED 
 switch:
    unique_id: esphome_switch
    name: "Esp Home Switch"
    state_topic: "esphome/eprint/tmpstate"
    command_topic: "esphome/eprint/state"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

ESP Home config for a ESP32

esphome:
  name: lolin32litemqttled
  friendly_name: lolin32litemqttled

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lolin32Litemqttled"
    password: "xxxxxxxxxxxxxxxxxxx"

captive_portal:
    
switch:
  - platform: gpio
    pin:
      number: 22
      mode: output
    id: blueLED
  - platform: template
    name: "eprint"
    id: eprint_enable
    optimistic: on

interval:
  - interval: 1000ms
    then:
      if:
        condition:
          switch.is_on: eprint_enable
        then:
          - switch.toggle: blueLED

mqtt:
  broker: 192.168.1.2
  on_message:
  - topic: esphome/eprint/state
    qos: 0
    payload: "OFF"
    then:
      - switch.turn_off: eprint_enable
  - topic: esphome/eprint/state
    qos: 0
    payload: "ON"
    then:
      - switch.turn_off: eprint_enable

Mikrotik and Home Assistant

A friend of mine remembered me of the Mikrotik addons in HA.
I forgot all about this.

I’ve got two integrations running. (Installed via HACS)

Don´t forget to enable the API on your devices.

/ip services
set api disabled=no port=8728

The addons/integrations extract a lot of information from the MT devices.

This integration will also let you know that your MT Router can be updated.

This is part if the presence detector/Device locator.
My roaming wifi network will give false locations. My laptop never left the same spot!

I will add to this page, after testing for a while

Domoticz + NodeRed + HomeAssistant MQTT

UPDATE 20240122 : below

I’ve got my RFXCOM connected to my Domoticz, and not connected to my HA.

RFXCOM 433 Mhz

To have the changes of my IOT devices being known to HomeAssistant I wanted to use mqtt.

Domoticz writes the mqtt topic (payload) like this
(topic: domoticz/out)

{
	"Battery" : 100,
	"LastUpdate" : "2022-11-17 18:21:59",
	"RSSI" : 6,
	"description" : "",
	"dtype" : "Temp + Humidity",
	"hwid" : "4",
	"id" : "230E",
	"idx" : 8461,
	"name" : "Living Temp/Hum",
	"nvalue" : 0,
	"stype" : "Cresta, TFA TS34C",
	"svalue1" : "20.0",
	"svalue2" : "60",
	"svalue3" : "1",
	"unit" : 1
}

So you can’t see which device it is by the topic name, the idx is in the json (8461)

To get this device ( a temperature sensor) into HA using mqtt i rewrote the mqtt topic using NodeRed

Temperature Humidity Sensor

NodeRed flow

[
    {
        "id": "836cc419ddb2ca2b",
        "type": "mqtt in",
        "z": "cfb00976f06591d6",
        "name": "",
        "topic": "domoticz/out/#",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "8c74c5f6.9a7a48",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 200,
        "y": 140,
        "wires": [
            [
                "dbe628e18027a287"
            ]
        ]
    },
    {
        "id": "a6cb517fd1cc9987",
        "type": "mqtt out",
        "z": "cfb00976f06591d6",
        "name": "",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "8c74c5f6.9a7a48",
        "x": 870,
        "y": 140,
        "wires": []
    },
    {
        "id": "dbe628e18027a287",
        "type": "function",
        "z": "cfb00976f06591d6",
        "name": "MQTT REWRITE",
        "func": "var varidx = msg.payload.idx;\nmsg.topic = \"home/\"+varidx+\"/payload\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 570,
        "y": 140,
        "wires": [
            [
                "a6cb517fd1cc9987"
            ]
        ]
    },
    {
        "id": "8c74c5f6.9a7a48",
        "type": "mqtt-broker",
        "name": "MQTTSERVERIP",
        "broker": "MQTTSERVERIP",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "15",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

The function node code ( rewrites the mqtt topic)

var varidx = msg.payload.idx;
msg.topic = "home/"+varidx+"/payload";
return msg;

Now we have a idx separated payload “home/IDX/payload”
(The other entries svalue/nvalue are from testing.

Adding the MQTT entries to HomeAssistant configuration.conf

mqtt:
 sensor:
  - name: "LivingTemperature"
    state_topic: "home/8461/payload"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.svalue1 }}"
  - name: "LivingHumidity"
    state_topic: "home/8461/payload"
    unit_of_measurement: "%"
    value_template: "{{ value_json.svalue2 }}"

Now i can add the card to the dashboard.

This should now be a generic mqtt translator

A quick update:

Adding a motion sensor:

Home Assistant
  - name: "LivingMotion"
    state_topic: "home/45/payload"
    value_template: "{{ value_json.nvalue }}"

Add sensors to be reset to the switch node

[
    {
        "id": "836cc419ddb2ca2b",
        "type": "mqtt in",
        "z": "cfb00976f06591d6",
        "name": "",
        "topic": "domoticz/out/#",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "8c74c5f6.9a7a48",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 200,
        "y": 140,
        "wires": [
            [
                "dbe628e18027a287",
                "768bb6e4b6731436"
            ]
        ]
    },
    {
        "id": "a6cb517fd1cc9987",
        "type": "mqtt out",
        "z": "cfb00976f06591d6",
        "name": "",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "8c74c5f6.9a7a48",
        "x": 870,
        "y": 140,
        "wires": []
    },
    {
        "id": "dbe628e18027a287",
        "type": "function",
        "z": "cfb00976f06591d6",
        "name": "MQTT REWRITE",
        "func": "var varidx = msg.payload.idx;\nmsg.topic = \"home/\"+varidx+\"/payload\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 570,
        "y": 140,
        "wires": [
            [
                "a6cb517fd1cc9987"
            ]
        ]
    },
    {
        "id": "768bb6e4b6731436",
        "type": "switch",
        "z": "cfb00976f06591d6",
        "name": "",
        "property": "payload.idx",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "45",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 330,
        "y": 260,
        "wires": [
            [
                "e46fed76a596a719"
            ]
        ]
    },
    {
        "id": "e46fed76a596a719",
        "type": "delay",
        "z": "cfb00976f06591d6",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 500,
        "y": 260,
        "wires": [
            [
                "3f7eeffc8d066de3"
            ]
        ]
    },
    {
        "id": "3f7eeffc8d066de3",
        "type": "function",
        "z": "cfb00976f06591d6",
        "name": "nvalue 0",
        "func": "msg.payload.nvalue = 0;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 260,
        "wires": [
            [
                "dbe628e18027a287"
            ]
        ]
    },
    {
        "id": "8c74c5f6.9a7a48",
        "type": "mqtt-broker",
        "name": "MQTTIP",
        "broker": "MQTTIP",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "15",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

UPDATE 20240122

I’m using domoticz as a 433->mqtt bridge, and some virtual devices i can toggle with curl (bash scripts)

I needed to make a custom 433 door sensor in Home Assistant with toggles to OFF after a few seconds. (There is NO off signal in this cheap sensor i’m using)

I’m changing the payload complete, to have a payload which matches the device class for door: (state with on/off)
It was nvalue = 0/1

(Whenever the IDX changes, I only have to update this Nodered part)
HA won’t notice the change.

var nvalue = msg.payload.nvalue;
msg.payload = {};
if(nvalue == 1)
{
msg.payload.state = "ON";
return msg;
} 

AND after 5 seconds

msg.payload = {};
msg.payload.state = "OFF";
return msg;

Node-RED Deploy
Export nodes
Export
[
    {
        "id": "d8a1af40d14307a9",
        "type": "mqtt in",
        "z": "a8ec6104.cb08c",
        "name": "",
        "topic": "home/35/payload",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "8c74c5f6.9a7a48",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 220,
        "y": 720,
        "wires": [
            [
                "ac3f66d770bb8f56"
            ]
        ]
    },
    {
        "id": "31cb1ca9e4060710",
        "type": "delay",
        "z": "a8ec6104.cb08c",
        "name": "",
        "pauseType": "delay",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 660,
        "y": 760,
        "wires": [
            [
                "dbf50e2146f5e631"
            ]
        ]
    },
    {
        "id": "96d60ca0976c555c",
        "type": "mqtt out",
        "z": "a8ec6104.cb08c",
        "name": "",
        "topic": "home/frontdoor/payload",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "8c74c5f6.9a7a48",
        "x": 1010,
        "y": 720,
        "wires": []
    },
    {
        "id": "ac3f66d770bb8f56",
        "type": "function",
        "z": "a8ec6104.cb08c",
        "name": "If open",
        "func": "var nvalue = msg.payload.nvalue;\nmsg.payload = {};\nif(nvalue == 1)\n{\nmsg.payload.state = \"ON\";\nreturn msg;\n} \n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 720,
        "wires": [
            [
                "31cb1ca9e4060710",
                "96d60ca0976c555c"
            ]
        ]
    },
    {
        "id": "dbf50e2146f5e631",
        "type": "function",
        "z": "a8ec6104.cb08c",
        "name": "Send Close",
        "func": "msg.payload = {};\nmsg.payload.state = \"OFF\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 810,
        "y": 760,
        "wires": [
            [
                "96d60ca0976c555c"
            ]
        ]
    },
    {
        "id": "8c74c5f6.9a7a48",
        "type": "mqtt-broker",
        "name": "MQTTSERVER",
        "broker": "MQTTSERVER",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "15",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

HA Part : Instead of the generic setup above (Old post part)

mqtt:
 binary_sensor:
  - name: "FrontDoor"
    state_topic: "home/frontdoor/payload"
    value_template: "{{ value_json.state }}"
    device_class: door

Home Assistant/NodeRed update

Upgraded the 2nd (Main) nodered machine to 3.0
Now the node-red-contrib-home-assistant-websocket nodes work again.

Also updated HA, and added some iframes using below.
The HA update broke my mqtt lighting due to changes in the code.

Below also in the example some iframe entries.
( When adding grafana, you need to change grafana.ini

allow_embedding = true

mqtt:
 broker: mqttbrokerserver <--- still errors on this part, 
 discovery: true   <--- but where to configure?
 light:                             <--- Moved this under mqtt
  - name: "KitchenOutside"          <--- added a dash
    payload_on: "99"
    payload_off: "0"
    unique_id: "KitchenOutside"
    brightness_scale: "99"
    brightness_state_topic: ha433/kitchenoutside/brightcontrol
    brightness_command_topic: ha433/kitchenoutside/brightcontrol
    state_topic: ha433/kitchenoutside/brightcontrol
    command_topic: ha433/kitchenoutside/control
    optimistic: false
    on_command_type: brightness

panel_iframe:                     <--- iframes added
  nodered:
    title: "NodeRed"
    url: "http://noderedserver:1880/"
    icon: si:nodered
    require_admin: true
  domoticz:
    title: "Domoticz"
    url: "http://domoticzserver:8080/"
    icon: si:homeadvisor
    require_admin: true
    
influxdb:
    host: influxserver
    port: 8086
    database: homeassistant
    max_retries: 3
    default_measurement: state <--- changed this
    

HomeAssistant add long-lived access token:

  • Click you profile icon bottom right
  • Scroll all the way down and press create token

Node-red

Add a HA node to a flow, double click and add a server with the little pencil

Base URL is important, you can’t just add http://internalip:port
Use a the URL configured in HA (config/network)