Sonoff ifan03 and changing entity state with Automations

Hello,

Newbie question !

I have my ifan03 working perfectly with the expert help by finity and the peeps on this thread https://community.home-assistant.io/t/ifan03-with-tasmota-to-ha/127599/97

I need help integrating the ifan03 in an Automation.

I have tried changing the state of the light or fan by using an Action automation and trying to change the state. I was trying on / off for the light. I was trying 1/2/3/4 for the fan.

Code is below of the device as it appears in config.yaml

Any guidance would be amazing !!

Thank You
Jason

fan:
  - platform: mqtt  
    name: "Music Fan"
    command_topic: "cmnd/fanmusic/FanSpeed"
    speed_command_topic: "cmnd/fanmusic/FanSpeed"    
    state_topic: "stat/fanmusic/RESULT"
    speed_state_topic: "stat/fanmusic/RESULT"
    state_value_template: >
      {% if value_json.FanSpeed is defined %}
        {% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}
      {% else %}
        {% if states.fan.fanmusic.state == 'off' -%}0{%- elif states.fan.fanmusic.state == 'on' -%}4{%- endif %}
      {% endif %}
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/fanmusic/LWT
    payload_off: "0"
    payload_on: "4"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    payload_available: Online
    payload_not_available: Offline
    speeds:
      - off
      - low
      - medium
      - high
light:
  - platform: mqtt
    name: "Music Light"
    command_topic: "cmnd/fanmusic/power1"
    state_topic: "stat/fanmusic/POWER1"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

using “on”/“off” for the light should work because those are the valid states for a light entity.

for the fan it gets a little bit trickier because it has both a state ("on/“off”) and a speed attribute (in your case the speeds are “low”, “medium” and “high” - not “1”, “2”, “3”).

The valid states for the fan are still only "on/“off” - not “0” or “4”. those are only for use in the template from Tasmota to get the correct state.

So you can “turn_on”/“turn_off” the fan via those services. turn_on will turn the fan on to the last speed it was set to according to the “speed:” attribute.

or you can set the speed directly by using the “set_speed” service which will both turn on the fan and set its speed to the desired value.

Yet again Finity to the rescue …

OK- got the light working by choosing Action Type - Call Service …

Tried the same with the fan … Action Type - Call Service - no luck no matter what I put in there. ‘fan.turn_on’, or ‘fan.turn_off’ . I can’t even get the ‘fan.set_speed’ to work as it won’t allow me to save the automation when I enter the ‘speed: low’ even when editing in YAML …

Funny thing is … Your control works perfect !

I really have no idea what is going on.

J

Sorry but you’ll have to explain that more clearly.

Where are you trying to put the data into a service call? In the automation UI editor?

Is the editor reporting an error?

EDIT: I just tried it with the editor and it worked OK for me for both the turn_on service and the set_speed service.

My bad … yes I am working in the automation UI editor.

Whilst there is no error being thrown up … it will not allow me to save the automation(the floppy dick icon at bottom right will not disappear when pressed).

I also tried calling the service via the Script UI … then I get this message
“Message malformed: extra keys not allowed @ data[‘sequence’][0][‘speed’]”

I think something is screwy with my build … I’m going to do a clean rebuild and try again …

Thanks again for your efforts in trying Finity …I’ll tackle it again tonight with the new build

J

I’m going crazy … had the rebuild and same issue … just to be super clear …I cannot save this automation rule with the following:

strange thing is I can get the below to work fine under the dev tools section

however this does not work under the dev tools section

Maybe it has to do with my config.yaml entry and the config of my ifan03 in Tasmota?

‘speed : fast’ ?? Is it a typo, since you declared speed : high everywhere?
speed:fast is not defined in your config yaml.
Also i think both entity_id and speed should reside inside data{} section in YAML.

1 Like

In the first screenshot above the likely reason it won’t save is because the yaml is not correctly formatted.

service: fan.set_speed
data:
  entity_id: fan.boys_fan
  speed: fast

And as was pointed out the speed should probably be ‘high’ not ‘fast’.

thank you febalci - you are right !

Thank you again Finity - you have helped enormously with the formatting ! It works !

One more clarification …on another sonoff ifan03 thread you mentioned that you revcently updated your config.yaml file because of 0 and 4 state settings etc … Can you please share your current fan settings so I can learn from that please?

Thank you again for your patience !

Sure!

Here ya go:

  ## iFan03 Tasmota
  - platform: mqtt  
    name: "Sunroom Fan"
    command_topic: "cmnd/iFan03-1/FanSpeed"
    speed_command_topic: "cmnd/iFan03-1/FanSpeed"    
    state_topic: "stat/iFan03-1/RESULT"
    speed_state_topic: "stat/iFan03-1/RESULT"
    state_value_template: >
      {% if value_json.FanSpeed is defined %}
        {% if value_json.FanSpeed == 0 -%}off{%- elif value_json.FanSpeed > 0 -%}on{%- endif %}
      {% else %}
        {% if states.fan.sunroom_fan.state == 'off' -%}off{%- elif states.fan.sunroom_fan.state == 'on' -%}on{%- endif %}
      {% endif %}
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/iFan03-1/LWT
    payload_off: "off"
    payload_on: "on"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    payload_available: Online
    payload_not_available: Offline
    speeds:
      - "off"
      - low
      - medium
      - high

the other config works with the iFan02 and it also works with my entity row (I think…) but it didn’t work for the standard pop-up window control. At least I think that’s how it was failing…I can’t find the posts to verify that for some reason.

Either way the code I posted above should work fine…I think :wink: