Automate MQTT switches?

Hi.
A newbie wondering how I’d achieve my goals:
I’d like to automatically cycle waterpump every 4 hours for ex. 3 minutes, turn lights on every morning and turn lights off every evening using esp12e.

Switches are working just fine, but I can’t get automation part working, could someone give a hand here?
My configuration syntax seems right, but it doesn’t work, no automated data is passed, even when trigger by hand.

    mqtt:
      broker: 127.0.0.1

    switch ebbpump:
      platform: mqtt
      name: 'EBB Pump'
      state_topic: 'ha/pump'
      command_topic: 'ha/pump'
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      optimistic:
      false retain: true

    automation ebbpump: //CYCLE PUMP EVERY 4 HOURS FOR 3 MINUTES
      alias: 'Waterpump controller'
      trigger:
        platform: time
        hours: '/4'
        minutes: 00
        seconds: 00
      action:
        entity_id: switch.ebbpump
        service: homeassistant.turn_on

    switch ebblight:
      platform: mqtt
      name: 'EBB Light'
      state_topic: 'ha/light'
      command_topic: 'ha/light'
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      optimistic:
      false retain: true

    automation ebblight_on: //TURN LIGHT ON
      alias: 'Light ON timer'
      trigger:
        platform: time
        after: '08:00:00'
      action:
        entity_id: switch.ebblight
        service: homeassistant.turn_on

    automation ebblight_off: //TURN LIGHT OFF
      alias: 'Light OFF timer'
      trigger:
    platform: time
    after: '20:59:59'
      action:
        entity_id: switch.ebblight
        service: homeassistant.turn_off

    sensor ebbtemp:
      platform: mqtt
      name: "EBB Temperature"
      state_topic: "sensor/temperature"
      qos: 0
      unit_of_measurement: "ºC"

    sensor ebbhumi:
      platform: mqtt
      name: "EBB Humidity"
      state_topic: "sensor/humidity"
      qos: 0
      unit_of_measurement: "%"

Hi,
you could try …

action:
  - service: homeassistant.turn_on
    entity_id: switch.ebbpump

I’m not sure if it would make a great deal of difference but you never know :stuck_out_tongue:

Thanks keithh666, but no luck…

I got this, when triggered Light ON manually:

Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=automation, service_data=entity_id=automation.light_on_timer, service_call_id=3052959760-22, service=trigger>
Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.components.automation] Executing Light ON timer
Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: domain=automation, name=Light ON timer, entity_id=automation.light_on_timer, message=has been triggered>
Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.helpers.script] Script Light ON timer: Running script
Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.helpers.script] Script Light ON timer: Executing step call service
Mar 26 10:51:12 hassbian hass[12818]: 17-03-26 10:51:12 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=homeassistant, service_data=entity_id=['switch.ebblight'], service_call_id=3052959760-23, service=turn_on>
Mar 26 10:51:13 hassbian hass[12818]: 17-03-26 10:51:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=switch, service_data=entity_id=['switch.ebblight'], service_call_id=3052959760-24, service=turn_on>
Mar 26 10:51:13 hassbian hass[12818]: 17-03-26 10:51:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=3052959760-24>
Mar 26 10:51:13 hassbian hass[12818]: 17-03-26 10:51:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=3052959760-23>
Mar 26 10:51:13 hassbian hass[12818]: 17-03-26 10:51:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.light_on_timer, new_state=<state automation.light_on_timer=on; friendly_name=Light ON timer, last_triggered=2017-03-26T13:51:13.066641+03:00 @ 2017-03-26T13:38:05.157401+03:00>, old_state=<state automation.light_on_timer=on; friendly_name=Light ON timer, last_triggered=2017-03-26T13:39:42.717866+03:00 @ 2017-03-26T13:38:05.157401+03:00>>
Mar 26 10:51:13 hassbian hass[12818]: 17-03-26 10:51:13 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=3052959760-22>

Hmmm, looks like it worked fine, so is the MQTT broker running and has the esp12e that controls the pump/lights subscribed to the correct topics?

When I use manual switches, I get “PUMP ON” and “LIGHT ON” in Arduino IDE serial console wich is attached to esp12e,
they are working just fine.
Automated part is not, I just noticed in logbook “Waterpump controller has been triggered” hourly, but no messages in Arduino IDE serial console.

Is the broker running and is the esp connected to the broker?

Can you manually post a mqtt message to the esp and see a result? You can use the command line to send a message. mosquitto_pub -t ‘ha/light’ -m ‘ON’ should turn the light on?

I saw something else both of your switches have a mistake at the end … . it may just be the way you formatted them for the forum but …

  optimistic:
  false retain: true

should be ....

  optimistic: false 
  retain: true

Broker is running and esp is connected.

Light and pump are not attached to esp12e atm, but “mosquitto_pub -t ‘ha/light’ -m ‘ON’'” and “mosquitto_pub -t ‘ha/pump’ -m ‘ON’” should work at hardware level, because I get response to serial monitor.

I’ll check issue You mentioned, but I think it’s because way formatted in forum… (EDIT: This was right in configuration file, my mistake during posting to forum)

I’m assuming you’ve run this thru’ the config yaml checker? and it comes out with no errors?

Since it seems that HA isn’t communicating with the broker or the yaml is faulty in some way.

Hmm my mqtt config looks like this…

mqtt:
  broker: 192.168.1.7
  port: 1883
  client_id: home-assistant
  username: !secret mqtt_usern
  password: !secret mqtt_pw
  keepalive: 60
  protocol: 3.1.1

Maybe you need to specify the port, protocol and broker IP addr.

Are you getting anything back for the two sensors?

Can you post your corrected automations please?

You haven’t mentioned what HW you are running on or what version of HA you are running?

To test the pump automation I would change it to every 4 mins and put a notify message in the action, to see if it gets triggered at all.

Here is my full configuration.yaml.

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 60.2257
  longitude: 24.8636
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 20
  # metric for Metric, imperial for Imperial
  unit_system: metric
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: Europe/Helsinki

# Show links to resources in log and frontend
introduction:

# Enables the frontend
frontend:

# Enables configuration UI
config:

http:
  # Uncomment this to add a password (recommended!)
  # api_password: PASSWORD
  # Uncomment this if you are using SSL or running in Docker etc
  # base_url: example.duckdns.org:8123

# Checks for available updates
updater:

# Discover some devices automatically
discovery:

# Allows you to issue voice commands from the frontend in enabled browsers
conversation:

# Enables support for tracking state changes over time.
history:

# View all events in a logbook
logbook:

# Track the sun
sun:

# Weather Prediction
sensor:
  platform: yr

# Text to speech
tts:
  platform: google

group: !include groups.yaml

device_tracker:
  - platform: ddwrt
    host: 192.168.1.2
    username: user
    password: pass
    interval_seconds: 10
    consider_home: 180
    track_new_devices: yes

  - platform: bluetooth_tracker

mqtt:
  broker: 192.168.1.13
  port: 1883
  client_id: home-assistant
  username: user
  password: pass
  keepalive: 60
  protocol: 3.1.1

switch ebbpump:
  platform: mqtt
  name: 'EBB Pump'
  state_topic: 'ha/pump'
  command_topic: 'ha/pump'
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  retain: true

automation ebbpump:
  alias: 'Waterpump controller'
  trigger:
    platform: time
    hours: '/4'
    minutes: 00
    seconds: 00
  action:
    service: homeassistant.turn_on
    entity_id: switch.ebbpump

switch ebblight:
  platform: mqtt
  name: 'EBB Light'
  state_topic: 'ha/light'
  command_topic: 'ha/light'
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  retain: true

automation ebblight_on:
  alias: 'Light ON timer'
  trigger:
    platform: time
    after: '08:00:00'
  action:
    service: homeassistant.turn_on
    entity_id: switch.ebblight

automation ebblight_off:
  alias: 'Light OFF timer'
  trigger:
    platform: time
    after: '20:59:59'
  action:
    service: homeassistant.turn_off
    entity_id: switch.ebblight

sensor ebbtemp:
  platform: mqtt
  name: "EBB Temperature"
  state_topic: "sensor/temperature"
  qos: 0
  unit_of_measurement: "ºC"

sensor ebbhumi:
  platform: mqtt
  name: "EBB Humidity"
  state_topic: "sensor/humidity"
  qos: 0
  unit_of_measurement: "%"

sensor outtemp:
  platform: mqtt
  name: "Outside"
  state_topic: "sensor/temperature1"
  qos: 0
  unit_of_measurement: "ºC"

sensor outhumi:
  platform: mqtt
  name: "Outside"
  state_topic: "sensor/humidity1"
  qos: 0
  unit_of_measurement: "%"

sensor outrealfeel:
  platform: mqtt
  name: "Outside"
  state_topic: "sensor/realfeel1"
  qos: 0
  unit_of_measurement: "%"

logger:
  default: warning
  logs:
    homeassistant.components.device_tracker.mqtt: debug

Sensors are not working right now, I have issue with arduino code, trying to solve it too.
HA is running at Raspberry Pi 2, HA version is 0.40.2 Hassbian.

When I use manual EBB Light switch, I get response from esp12 to serial console.

Logbook tells me:
Light ON timer has been triggered,
but serial console doesn’t show anything…

On arduino code I have water level sensor, wich cuts water pump operation when water level is low in tank,
how I could send a response to HA if water level was too low when pump was triggered?

Hi Jonezy,

Thanks for posting your config :slight_smile:
I can’t see anything wrong with the config, it’s pretty much exactly the same as mine. Have you tried manually posting a mqtt message to the adruino using a putty console (at the HA side ie. mqtt broker side), something like this…

mosquitto_pub -d -u username -P yourpassword -t ha/light -m “ON”

This will test that you are communicating with the adruino via mqtt. Hopefully the light will come on or at least the serial console will show the result? Things to investigate, are you using the same case on both sides? (ON or on) and are you publishing/subscribing to the correct topics (ha/light or ha/pump note all lower case) on both sides?

I tend to split my command topic and state topic …

  state_topic: 'stat/light'
  command_topic: 'cmnd/light'

It just makes it easier to see where the messages are going to or coming from. The other thing to make sure of is leading “/” at the adruino end, so ‘ha/light’ is a different address to ‘/ha/light’.

As for the pump I assume you have a way of monitoring the water level, so I would check what level it is before triggering the pump and then send an appropriate mqtt message to HA.

Regards

Does this mean you can change the switches on the HA page and the pump turns on and off? This would mean that the MQTT is working fine and its the automation yaml that is wrong.

Yes, this is how it works.
It works from HA page and from command line just fine.

Can you change the name of the pump automation to ebbpump_on so it’s not the same name as the switch?

You mean same way as automation ebblight_on?
Light automation doesn’t work neither…

OK, have you tried manually triggering the automation from the automation card in the front-end, you have to click on the automation line then click the trigger, that should have the same effect as manually clicking the switch in the front end, since it bypasses the automation trigger. That will eliminate the actual turn on/off part of the automation. You can’t click the automation switch as that just turns the automation off/on.

Yes, as I told before manual triggering doesn’t work neither:

in my automation that works I have …

  - service: homeassistant.turn_on
    data:
      entity_id: switch.bathroom_light

So you might try adding data and indenting the entity_id by 2 spaces.

Doesn’t work:

automation ebbpump_on:
  alias: 'Waterpump controller'
  trigger:
    platform: time
    hours: '/4'
    minutes: 00
    seconds: 00
  action:
    - service: homeassistant.turn_on
      data:
        entity_id:  switch.ebbpump

I think you don’t need - service as you have the code in the config file not in a separate one like I have, so yours should look like …

    service: homeassistant.turn_on
    data:
      entity_id: switch.bathroom_light

The only other thing I can think of is changing
service: homeassistant.turn_on to service: switch.turn_on

Doesn’t work neither:

switch ebbpump:
  platform: mqtt
  name: 'EBB Pump'
  state_topic: 'ha/pump'
  command_topic: 'ha/pump'
  qos: 0
  payload_on: "ON"
  payload_off: "OFF"
  optimistic: false
  retain: true

automation ebbpump_on:
  alias: 'Waterpump controller'
  trigger:
    platform: time
    hours: '/4'
    minutes: 00
    seconds: 00
  action:
    service: switch.turn_on
    data:
      entity_id:  switch.ebbpump

This is getting impossible?