Passing MQTT payload into a delay and then some

Running home-assistant under docker on HypritOS. Works a treat.

Just learning this HA evironment though.

Learning task is that I have an ESPHome device I built and I can turn 4 relays on the board on/off from HA switches. Tested and working perfectly thanks.

What I want to do is send an MQTT topic to HA, on receipt HA turns on the ESPHome relay on (and therefore a relay on the board). Using the same MQTT topic, use the payload to set a HA timer/delay and then turn the ESPHome switch off after the delay/timer times out.

Using the developer MQTT tool I can currently turn the ESPHome switch on by publishing on the required MQTT topic, HA then turns the switch on via an Automation set by the HMI. So far so good.

Using the HA HMI to develop automations then, what is the means to take the MQTT payload (in milliseconds) and pass it to a delay (in seconds). I probably have the same problem with Scripts? That is I still need take the MQTT payload, pass it into a script, convert milliseconds to seconds and then set a delay and then an action to turn MQTT switch off.

In my configuration.yaml I have:

switch:
  - platform: mqtt
    name: MQTT Zone 1
    command_topic: "/os/12614780/1/1"
    value_template: '{{value_json.number / 1000}}'

“MQTT Zone 1” now appears in my widgets.

I built ESPHome automation and can toggle the MQTT Zone 1 switch at the HMI to toggle the ESPHome relay. Whoohoo!

I don’t seem to be able to get the MQTT Zone 1 switch to toggle via an MQTT topic using the setup in the configuration file.

I appear to be getting the error:

Value cannot be processed as a number: <state switch.mqtt_zone_1=off; friendly_name=MQTT Zone 1, assumed_state=True @ 2020-01-11T10:15:28.827275+10:30> (Offending entity: off)

So, the /1000 is trying to convert from milliseconds to seconds. The value was set to NUMBER in node-red. The actual topic with payload is:

/os/12614780/1/1/300000

I assume I am not understanding something about the value_template definition as explained in the associated help file.

Once I do get the value in the payload, I will want to get that into a timer that will turn the relay in the ESPhome off.

So and some point I will need to understand how to get the value out of the payload and into the delay.

I have dabbled with and automation as a numeric state with the MQTT switch as the entity delivering the value but I guess that won’t pan out until I get the payload value sorted.

So, first things first, any hints please on how to correct the value_template so that the topic actually delivers the value from the MQTT switch?

Cheers,
B

value_template: "{{ value_json.number|float / 1000 }}"

Ah, there was no guidance that one must convert to float, thanks as I was happy enough for int.

DOH!

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

switch:
  - platform: mqtt
    name: MQTT Zone 1
    command_topic: "/os/12614780/1/1"
    value_template: "{{ value_json.number | float / 1000 }}"

results in:

$ more .homeassistant/home-assistant.log
2020-01-11 12:36:22 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] expected '<document start>', but found
'<block mapping start>'
  in "/config/configuration.yaml", line 3, column 1
2020-01-11 12:36:22 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: expect
ed '<document start>', but found '<block mapping start>'
  in "/config/configuration.yaml", line 3, column 1

Tricky, since that part of the file was not change.

Worse, that is an old log report it seems (when the restart first failed after the edit of configuration.yaml).

I cannot start or restart HA using docker and none of the fails now seem reflected in the log. The command line just drops out. Likely has nothing to do with the switch configuration but something deeper that has occured. No means to sneak a peek in the container, since it won’t start/restart. So I will delete container and start from image again to see if that clears the problem.

Please post the sanitised contents of all included yaml files.

That would be the one above. This was a default setup so the others are empty and the wrong side of line 3.

Comment out the include lines if you’re not using them and try again.

Well no. As I indicated I deleted the container and started again. I did also note a throw away comment in a thread that the configuration.yaml was being deprecated the UI was supposed to be used? I am now finding:

Message malformed: Service does not match format <domain>.<name> for dictionary value @ data['action'][0]['service']

With an automation setup from the UI with the yaml of that being:

platform: mqtt
topic: /os/12614780/1/1

So that is with the default rpi image and a new container.

Not a promising start.

Seems like this is caused by touching the optional payload field of the automation trigger (if that were an MQTT). I tried 300000 and “300000” as the payload and got errors both times. I then deleted the payload string but the error persisted and I could not find a means to clear the error. The only thing that sorted that was creating a new automation and NOT TOUCHING the payload field in the UI.

Is there guidance on the format of data fields? I can seem to find anything on a search :frowning:

The formatting aside, it still seems to be a bug since the problem persists after the text is deleted.

Okay, with a bit more fiddling and reading, it seems that “value” represents the “payload” so I don’t need to use “value_json” at all.

So the 300000 value is actually 5 mins. What I opted to do is send “00:05” instead. That is:

/os/12614780/1/1/00:05

That way I was hoping to have 00:05 turn up in “value” and use “value” in the delay action in the automation.

I am still vague on how I might do that.

Am I to use an MQTT entity? The way it uses payload seems to be as a refinement of the trigger but not to pass a payload? MQTT switch? MQTT sensor? I got sensor reporting a value on change in the logging, but still couldn’t work out how to take the payload off the trigger and pass to a delay (for example). Is this straight forward OR is this against the HA idiom somehow? Or is this just a custom component I need write?

Cheers,
B