Modbus tcp info on entities

Goodmorning everyone.
I’m new to HA and I’m passionate about it.
Currently I have home automation controlled by a raspberry with codesys
and with 2 controllino.biz for the ignitions and various other operations.
Everything works perfectly.
The two controllino.biz them control via modbus tcp
Now I would like to pair HA to create the same automations and to understand whether to migrate definitively to HA.

I started with the modbus tcp protocol on HA.
I only control the lights by using an entity button to press twice to turn a lamp on and off and the lamp status is not displayed correctly.
I am attaching the part of the configuration code hoping for help.

I use Home assistant installation hot hass.io

Thanks, Alberto

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 45.5356
  longitude: 10.2147
  # Impacts weather/sunrise data (altitude above sea level in meters)
  elevation: 0
  # 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/Rome
  # Customization file
  customize: !include customize.yaml

# Enables the frontend
frontend:

# Enables configuration UI
config:

http:
 # Secrets are defined in the file secrets.yaml
 # api_password: !secret http_password
 # Uncomment this if you are using SSL/TLS, running in Docker container, etc.
 # base_url: example.duckdns.org:8123

# Checks for available updates
# Note: This component will send some information about your system to
# the developers to assist with development of Home Assistant.
# For more information, please see:
# https://home-assistant.io/blog/2016/10/25/explaining-the-updater/
updater:
 # Optional, allows Home Assistant developers to focus on popular components.
 # include_used_components: true

# Discover some devices automatically
discovery:

# Sensors
sensor:
  # Weather prediction
  - platform: yr

# 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:

# Enables a map showing the location of tracked devices
map:

# Track the sun
sun:

# Weather prediction
sensor:
  - platform: yr

# Text to speech
tts:
  - platform: google_translate

modbus:
  - type: tcp
    host: 10.0.0.45
    port: 502
    name: maxi

  - type: tcp
    host: 10.0.0.46
    port: 502
    name: mega
 
mqtt:
  broker: localhost
  username: mosquitto
  password: !secret mqtt_password
  discovery: true
  discovery_prefix: homeassistant
  
switch:
  - platform: mqtt
    name: "Basic tasmota"
    state_topic: "stat/basic/POWER"
    command_topic: cmnd/basic/POWER
    availability_topic: "tele/basic/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: modbus
    coils:
      - name: Switch1
        hub: maxi
        slave: 1
        coil: 1
      - name: Switch2
        hub: maxi
        slave: 1
        coil: 2
      - name: Switch3
        hub: maxi
        slave: 1
        coil: 3
      - name: Switch4
        hub: maxi
        slave: 1
        coil: 4
      - name: Switch5
        hub: maxi
        slave: 1
        coil: 5
      - name: Switch6
        hub: maxi
        slave: 1
        coil: 6
      - name: Switch7
        hub: maxi
        slave: 1
        coil: 7
      - name: Switch8
        hub: maxi
        slave: 1
        coil: 8
    registers:
      - name: Antecucina
        hub: maxi
        slave: 1
        register: 1
        command_on: 1
        command_off: 0
      - name: Letto1
        hub: maxi
        slave: 1
        register: 1
        command_on: 4
        command_off: 0
      - name: Corridoio 1
        hub: maxi
        slave: 1
        register: 1
        command_on: 20
        command_off: 0
      - name: Scala
        hub: maxi
        slave: 1
        register: 1
        command_on: 2
        command_off: 0
      - name: Sala 1
        hub: maxi
        slave: 1
        register: 1
        command_on: 8
        command_off: 0
      - name: Ingresso androne
        hub: maxi
        slave: 1
        register: 1
        command_on: 32
        command_off: 0
      - name: Apre tapparella sala 
        hub: maxi
        slave: 1
        register: 2
        command_on: 128
        command_off: 0
      - name: Chiude tapparella sala 
        hub: maxi
        slave: 1
        register: 2
        command_on: 64
        command_off: 0
      - name: Apre tapparella cucina 
        hub: maxi
        slave: 1
        register: 2
        command_on: 512
        command_off: 0
      - name: Chiude tapparella cucina 
        hub: maxi
        slave: 1
        register: 1
        command_on: 256
        command_off: 0
      - name: Cucina
        hub: mega
        slave: 1
        register: 1
        command_on: 8192
        command_off: 0
      - name: Sala
        hub: mega
        slave: 1
        register: 1
        command_on: 4096
        command_off: 0
      - name: Pareti sala
        hub: mega
        slave: 1
        register: 1
        command_on: 2048
        command_off: 0
      - name: Pilastro
        hub: mega
        slave: 1
        register: 1
        command_on: 1024
        command_off: 0
      - name: Corridoio esterno
        hub: mega
        slave: 1
        register: 1
        command_on: 1
        command_off: 0

        


device_tracker:
  - platform: ping
    interval_seconds: 30
    consider_home: 1200
    hosts:
      Alberto: 10.0.0.23
      Helga: 10.0.0.216

sensor:
  - platform: mqtt
    name: Sonda
    state_topic: "tele/basic/SENSOR"
    unit_of_measurement: '°C'
    value_template: '{{ value_json.DS18B20.Temperature }}'
 
group:
  living_room:
  - light.bowl
  - light.ceiling
  - light.tv_back_light
  entities:
  - light.bedroom
  - media_player.nexus_player
 
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

You may need to configure a modbus binary sensor to get the status of the light. Hope that helps.

Ok… I try
Thanks

I tryed, but whitout goal.
Take me help with example :blush:

Sorry but I very confused

Thanks, Alberto

In my case I use function block diagram (FBD) programs on my Micro PLC’s.

The ‘switch’ is toggled by HA toggling a digital flag. The result of that state change has caused my modbus switch to turn on OR off.
To read that state I use another digital flag to read that switch state.
That digital flag is the one that reports the switch state back to HA.

ok, so in a few words you tell me that I should always keep the plc management (in my case codesys) working and add HA in parallel?
But what if I just wanted HA?

Thanks, Alberto

Pretty much…I try to use HA as my monitoring device and program my PLC to do the nuts and bolts stuff. For devices which are not able to have their own program then HA takes on that role.
For your use then it’s up to you how you setup your system of course.

Good morning,
I’m back on the subject because I can’t understand how to visualize the status of an on / off button.
Every time I press the button the light turns on and for a moment the button is yellow but soon after Phrygian returns but the light stays on. If I press the button again the light is turned off and the yellow button returns to gray.
I use bistable relays and I get the relay status from a second contact that enters the plc from an input.
So if the input status is high the lamp is on and if it is low the lamp is off.
In HA I entered a register that at the push of a button writes the value on the Modbus to turn on the light.

    registers:
      - name: Test
        hub: maxi
        slave: 1
        register: 1
        command_on: 1
        command_off: 0

I then added a switch that reads the value of its input

   coils:
     - name: Test
       hub: maxi
       slave: 1
       coil: 1

So in HA I have a button and a switch and I see that those work correctly!
When I press the button, which writes in the modbus register, shortly after I see the switch turned on and permanent, if we repeat the button the switch goes off.

How can I create a code that shows the light status in one button?

Thanks, Alberto

I try to rephrase the question because I just can’t do what I would like … maybe impossible?
Then I have relays controlled via modbus.
The modbus command controls bistable relays.
The relays have 4 contacts
two contacts are used to switch on the lamp, the other two are used to know the status of the lamp ignition.
The lamp ignition status enters an input that reads the status.
If high the lamp is on if low is off.
In the home assistant I inserted a switch that turns on a lamp and a sensor that reads the status.
I would like the pressure of the switch in HA to illuminate the icon and remain so, but when I press the button the light turns on but the status of the icon after a short time goes off while the light is on.
While the icon of the state that controls the status remains on.
How can I do everything with a button?

Thanks, Alberto

@alverman Perhaps this image may help you see what I am trying to explain a little better?

From left to right:

Flag F8 is configured as my HA Modbus switch entity. I use it to toggle the state of my PLC Switch Device B013.

I can also toggle this locally via the digital input I006.

The output of the PLC Switch Device B013 turns the output high or low depending on its state.

I read that state via Flag F14 which is configured as my HA Modbus binary sensor entity.

I can also control field devices via Digital Output Q003.

Please let me know if that is any clearer?

Thanks Wellsy

I think, I say I think I understand what you mean.
If so, as I think I understand, but I use a step by step relay then the plc output does not remain high.
In fact modbus goes high with an impulse and the relay exchanges (on) then returns to zero, another impulse exchanges (off) and returns to zero.

Sorry but I see a flaw in my explanation…

The digital output is what would actually turn the light on or off in your setup (ie: your plc relay)
The Flag F14 which is configured as a binary sensor entity is giving you the on or off status of your light.

However I would always have a button (switch in HA) that turns on but the display would always be through another component (sensor in HA)
I would need the switch to do its duty but its icon would show the status of the sensor :wink: !!
I’ve been going around this thing for weeks and now I think it’s not possible with HA.
Too bad I’d miss only that to start replacing codesys with HA.
He has so many things that make me gorge but if I can’t do this for me it’s just, unfortunately, a waste of time.

Thanks anyway

The modbus implementation in HA is possibly not as ‘mature’ as other platforms such as MQTT say but I for one find it is reliable and I can live within its limitations.
HA has so many other benefits once you get it nutted out I would encourage you to try to live with some minor limitations.
I moved from a so called ‘professional’ SCADA product which had absolutely nothing on this fantastic product which by the way is still in beta state. Chin up keep plugging away!

PS: have you looked at node-red? It has some pretty nifty modbus Contrib’s which may be what you are looking for?

@alverman As an illustration here is a way I have learned to live with the limitations and at the end of the day (for me anyway) the solution seems to be more logical…?

In the case of my Grey Water Sump (my Control Panel).

The logic starts from the bottom working upwards to the pump icon:

  • I can set a limit on how many times I want the pump to run (the program resets it every 6 hours)
  • Then I can read the current runcount setpoint
  • If I wish and runcount trips I can reset it
  • Then I can see if other systems are not ready to receive grey water (inhibit on or off)
  • See if the runcount is tripped or not
  • HA Switch here Turn the system from Off to Auto NOTE: this is not a toggle switch. For process reasons I want the system to not restart if I lose power. It restarts in Off position.
  • HA binary sensor here Read that the system status is Auto or is Off (it is presently On so is Auto)
  • See the Level is above or below Low
  • See the Level is above or below High
  • See the pump is running or not
1 Like

Thanks Wellsy for your interest :grinning:
Actually I don’t want to abandon HA also because making small changes in codesys involves different problems.
While in HA it is simpler and more immediate.
Actually I also thought of node red, I installed it but at the moment it doesn’t work for me.
I’ll be in the tests because actually HA satisfies me 99%.
That little 1% is only given by my need to have a button that enables the icon based on a state.
I am confident: D

Thanks, Alberto

Cool…post your problems you are having (on a new post) with node-red and/or look up the videos on youtube which got me up and running easy enough. For my needs I have shut down node-red as I am looking more towards keeping everything running on HA alone.

But wouldn’t it be easier to modify entity button?
I ask !!
Where is the code of those components … I would like to put the nose :face_with_monocle:

You can look at modbus hub code here

Likewise modbus switch code here

I have solved here: https://community.home-assistant.io/t/entity-button-with-icon-state-on-value-of-sensor-on-off/121262/19

thank to finity !!