MQTT Light state template (0:off, 1-99:on)

I have a Way acting like a z-wave hub and like to look at implementing dimmer to HASS.

Z-way have the option to receive 0-99 and on/off but only sends out 0-99 as feedback.
So my question is, how can I use state template to control the status of the switch. Like to turn it off at 0 and on beetween 1-99.

thanks for any help.

{% if value == 0 %}off{% else %}on{% endif %}

should be something like what you need

thanks:)

It seems like its always on now. Maybe its never going in this if sentence?
I trying out the MQTT Light comp. its that the correct one?

On other thing is that when I’m sending the on command I wish to only send “on” not “on” then brightness before or after.

light:
  - platform: mqtt
    name: "WC Speil"
    state_topic: "zway/wC/wc_Speil_Lys"
    command_topic: "zway/wC/wc_Speil_Lys/set"
    brightness_state_topic: "zway/wC/wc_Speil_Lys"
    brightness_command_topic: "zway/wC/wc_Speil_Lys/set"
    brightness_scale: 99
#    on_command_type: last
    qos: 0
    payload_on: "on"
    payload_off: "off"
    optimistic: false
    state_value_template: "{% if value == 0 %}off{% else %}on{% endif %}"

Its hard to suggest anything without being certain of the messages. Please paste the messages being sent and received using something like mosquitto_sub see https://www.home-assistant.io/docs/mqtt/testing/

I’m jusing HASS, didn’t quiet understand how to add this message being sent function. But I can try to explain.

This is a dimmer from Z-way and I use a MQTT program to se what values being sent/received:
z-way -> HASS: 0-99
HASS -> 0-99, but z-way also se “on” and “off” from HASS, but do not send this when pressing on/off in z-way interface.

The reason for wanting to send the on or off from HASS is to use the “last value” when pressing on in HASS HMI.

I will try to fetch the data being sent:)

I did a bit of checking, and the value is a string, which you have to convert to an integer in jinja before comparing it. This seems to work - if I understand your messages correctly

    state_value_template: "{% if value|int == 0 %}off{% else %}on{% endif %}"

The messages I sent to test this are, so if I got this wrong I maybe barking up the wrong tree:grinning:


$ mosquitto_pub -t zway/wC/wc_Speil_Lys -m 1

$ mosquitto_pub -t zway/wC/wc_Speil_Lys -m 0

That did ut, thanks for help btw!:slight_smile:

Havent figured out how to fetch this data from MQTT when I’m using HASS. Can I use the Mac terminal to connect to HASS?

Here is something from my MQTTBox app, this is from when I try to turn it on from HASS:
If I send message from MQTTBox the Payload_type need to be set to: “Strings/JSON/XML/Characters”
As you can se it send value 66 then on when I press “on” in HASS. What can I do to only send “on”.

66

qos : 0, retain : false, cmd : publish, dup : false, topic : zway/wC/wc_Speil_Lys, messageId : , length : 24, Raw payload : 5454

on

qos : 0, retain : false, cmd : publish, dup : false, topic : zway/wC/wc_Speil_Lys/set, messageId : , length : 28, Raw payload : 111110

66

qos : 0, retain : false, cmd : publish, dup : false, topic : zway/wC/wc_Speil_Lys/set, messageId : , length : 28, Raw payload : 5454

Here is the link for the MQTT app in z-way:

It looks like the on_command_type parameter would set this, but there is no option to not send the brightness.

It shouldn’t matter as it will just set the brightness to the level indicated by HA.

I notice though, that you have the same topic to set the brightness and the on state, which doesn’t look correct. Doesn’t your controller have different topics for that?

the topic is the same for on/off and brightness commands according to the github link. Its sort of working now, but if I set the on_command_type to brightness the switch in HA set the light to 99% and not last value.
So I tried the “last” command but then the light jumps to the value I have set but then goes back again to old value. Think this has something to do with the update of z-way isn’t fast enough before receiving the “on” value.

Maybe the “MQTT Template” component have more options. Really like to have a solid MQTT before I move everything to HASS:)

Tried to set the command to “first” and it seems pretty stable now:)

Thanks a lot for all the help!

1 Like